<template>
  <div>
    <el-form :inline="true" :model="form" label-width="80px" class="demo-form-inline" align="center">
      <el-form-item label="用户姓名:">
        <el-input v-model="form.trueName" placeholder="请输入用户姓名"></el-input>
      </el-form-item>
      <el-form-item label="用户编号:">
        <el-input v-model="form.id" placeholder="请输入用户编号"></el-input>
      </el-form-item>
      <el-form-item label="账号状态:">
        <el-select v-model="form.status" placeholder="请选择账号状态">
          <el-option
            v-for="item in userStatus"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="用户权限:">
        <el-select v-model="form.roleId" placeholder="用户权限">
          <el-option
            v-for="item in userRole"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit()">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button @click="resetForm()">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table
      :data="tableData.slice((currentPage-1)*pageSize,currentPage*pageSize)"
      border
      align="center"
      style="width: 100%;">
      <el-table-column
        label="#"
        type="index"
        width="50"
        align="center">
      </el-table-column>
      <el-table-column
        prop="id"
        label="用户编号"
        width="300"
        align="center">
      </el-table-column>
      <el-table-column
        prop="trueName"
        label="用户姓名"
        width="120"
        align="center">
      </el-table-column>
      <el-table-column
        prop="username"
        label="用户名"
        width="120"
        align="center">
      </el-table-column>
      <el-table-column
        prop="status"
        label="账号状态"
        width="120"
        align="center">
        <template slot-scope="scope">
          <span>{{ $formatDate.formatStatus(scope.row.status) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="roleId"
        label="用户权限"
        width="120"
        align="center">
        <template slot-scope="scope">
          <span>{{ $formatDate.formatRole(scope.row.roleId) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="createTime"
        label="创建日期"
        width="250"
        align="center">
      </el-table-column>
      <el-table-column
        label="操作"
        width="100"
        align="center">
        <template slot-scope="scope">
          <el-button @click="modifyUser(scope.row)" type="text">修改</el-button>
          <el-button @click="deleteUser(scope.$index,tableData)" type="text">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="block" style="margin-top:15px;">
      <el-pagination align='center' @size-change="handleSizeChange" @current-change="handleCurrentChange"
                     :current-page="currentPage"
                     :page-sizes="[1,5,10,20]"
                     :page-size="pageSize"
                     layout="prev, pager, next"
                     background
                     :total="tableData.length">
      </el-pagination>
      <el-dialog title="修改用户信息" :visible.sync="modifySync" width="24%">
        <el-form ref="modifyForm" label-width="80px" :model="modifyForm" :rules="modifyRole">
          <el-form-item label="用户编号" prop="id">
            <el-input v-model="modifyForm.id" disabled style="width: 220px"></el-input>
          </el-form-item>
          <el-form-item label="用户姓名" prop="trueName">
            <el-input v-model="modifyForm.trueName" style="width: 220px"></el-input>
          </el-form-item>
          <el-form-item label="用户名" prop="username">
            <el-input v-model="modifyForm.username" style="width: 220px"></el-input>
          </el-form-item>
          <el-form-item label="账号状态:">
            <el-select v-model="modifyForm.status" placeholder="请选择账号状态">
              <el-option
                v-for="item in userStatus"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="用户权限:">
            <el-select v-model="modifyForm.roleId" placeholder="用户权限">
              <el-option
                v-for="item in userRole"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button type="primary" @click="(modifySync = false), modifySubmit('modifyForm')">确 定</el-button>
          <el-button @click="modifySync = false">取 消</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>


export default {
  created () {
    this.getUserList()
  },
  watch: {},
  methods: {
    modifySubmit (modifyForm) {
      const _this = this
      this.$refs[modifyForm].validate((valid) => {
        if (valid) {
          this.$confirm("此操作将永久修改信息, 是否继续?", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }).then(async () => {
            await this.$http.userMessage.update(this.modifyForm).then(function (res) {
              if (res.code == 2000) {
                _this.modifySync = false
                _this.$message({
                  type: "success",
                  message: "修改成功!"
                })
              } else {
                _this.$message({
                  type: "error",
                  message: res.msg
                })
              }
            })
            await this.getUserList()
          }).catch(() => {
            this.$message({
              type: "info",
              message: "已取消修改"
            })
          })
        } else {
          alert("error submit!")
          return false
        }
      })
    },
    modifyUser (row) {
      const _this = this
      this.modifySync = true
      this.$http.userMessage.findOne(row.id).then((res) => {
        if (res.code == 2000) {
          _this.modifyForm = res.data
        }
      })
    },
    deleteUser (index,row) {
      row.splice(index, 1)
    },
    formatDate (vaule, val) {
      // return formatDate(vaule,val)
    },
    formatName (value) {
      // return Desensitization.chineseName (value);
    },
    formatIdNo (idCard) {
      // return Desensitization.idCard (idCard);
    },
    handleSizeChange (val) {
      this.currentPage = 1
      this.pageSize = val
    },
    handleCurrentChange (val) {
      this.currentPage = val
    },

    getUserList () {
      const _this = this
      this.$http.userMessage.findAll().then((res) => {
        if (res.code == 2000) {
          _this.userList = res.data
          _this.tableData = _this.userList
        }
      })
    },
    onSubmit () {
      this.tableData = []
      let isTrue = true
      for (let i = 0; i < this.userList.length; i++) {
        isTrue = true
        if (this.form.trueName != null) {
          if (this.form.trueName != this.userList[i].trueName) {
            isTrue = false
          }
        }
        if (this.form.id != null) {
          if (this.form.id != this.userList[i].id) {
            isTrue = false
          }
        }
        if (this.form.status != null) {
          if (this.form.status != this.userList[i].status) {
            isTrue = false
          }
        }
        if (this.form.roleId != null) {
          if (this.form.roleId != this.userList[i].roleId) {
            isTrue = false
          }
        }
        if (isTrue) {
          this.tableData.push(this.userList[i])
        }
      }
    },
    resetForm () {
      this.form = {}
      this.tableData = this.userList
    }
  },

  data () {
    return {
      modifyForm: {},
      modifyRole: {
        trueName: [
          { required: true, message: "请输入用户姓名", trigger: "change" },
          { min: 2, max: 5, message: "长度在 2 到 5 个字符", trigger: "change" }
        ],
        username: [
          { required: true, message: "请输入用户名", trigger: "change" },
          { min: 2, max: 16, message: "长度在 2 到 16 个字符", trigger: "change" }
        ],
        status: [
          { required: true, message: "请选择账号状态", trigger: "change" }
        ],
        roleId: [
          { required: true, message: "请选择用户权限", trigger: "change" }
        ]
      },
      modifySync: false,
      userStatus: [
        {
          value: 1,
          label: "正常"
        },
        {
          value: 2,
          label: "禁用"
        }
      ],
      userRole: [
        {
          value: 1,
          label: "学生"
        },
        {
          value: 2,
          label: "教师"
        },
        {
          value: 3,
          label: "管理员"
        }
      ],
      currentPage: 1, // 当前页码
      total: 20, // 总条数
      pageSize: 15, // 每页的数据条数
      outpatRegistList: [],
      userList: [],
      value1: false,
      form: {},
      tableData: []
    }
  }

}
</script>
